#include "cScene.h"
#include "cMouse.h"
#include <cstdio>

cScene::cScene(void)
{
	cx = 0;
	cy = 10;
	map = NULL;
	walkabilityMap = NULL;
	waterAnimation = 0;
	waterDelay = 0;

	// Editor mode:
	selx = 0;
	sely = 0;
}
cScene::~cScene(void)
{
	if (map) 
	{
		delete[] map;
		map = NULL;
	}
	if (walkabilityMap) 
	{
		delete[] walkabilityMap;
		walkabilityMap = NULL;
	}
}

void cScene::LoadMap(char *file)
{
	FILE *f = NULL;
	f = fopen(const_cast<const char*>(file), "r");
	//El primer entero indica el lado del cuadrado.
	fscanf(f,"%d",&sceneArea);

	map = new int[sceneArea * sceneArea];
	walkabilityMap = new int[sceneArea * sceneArea];

	//Leemos Los id de textura del mapa
	for(int i = 0, k = 0, n; i < sceneArea; ++i)
	{
		for(int j = 0; j < sceneArea; ++j, ++k)
		{
			fscanf(f, "%d", &n);
			map[k] = n;
		}
	}

	//Leemos el walkabilityMap
	for(int i = 0, k = 0, n; i < sceneArea; ++i)
	{
		for(int j = 0; j < sceneArea; ++j, ++k)
		{
			fscanf(f, "%d", &n);
			walkabilityMap[k] = n;
		}
	}

	fclose(f);
}

void cScene::SaveMap(char *file)
{
	FILE *f;
	f = fopen(const_cast<const char*>(file), "w");
	//El primer entero indica el lado del cuadrado.
	fprintf(f, "%d\n",sceneArea);
	
	//Escribimos Los id de textura del mapa
	for(int i = 0, k = 0; i < sceneArea; ++i)
	{
		for(int j = 0; j < sceneArea; ++j, ++k)
		{
			fprintf(f, "%d ", map[k]);
		}
		fprintf(f, "\n ");
	}
	
	//Escribimos el walkabilityMap
	for(int i = 0, k = 0; i < sceneArea; ++i)
	{
		for(int j = 0; j < sceneArea; ++j, ++k)
		{
			fprintf(f, "%d ", walkabilityMap[k]);
		}
		fprintf(f, "\n ");
	}

	fflush(f);
	fclose(f);
}

void cScene::GetMap(int **map)
{
	*map = this->map;
}

void cScene::GetWalkabilityMap(int **walkabilityMap)
{
	*walkabilityMap = this->walkabilityMap;
}
void cScene::GetSceneOrigin(int *cx, int *cy)
{
	*cx = this->cx;
	*cy = this->cy;
}

int cScene::GetWaterAnimation(void)
{
	return waterAnimation;
}

void cScene::GetSelectedTile(int *x, int *y)
{
	*x = this->selx;
	*y = this->sely;
}

void cScene::SetSelectedTile(int selx, int sely)
{
	this->selx = selx;
	this->sely = sely;
}

int cScene::GetSceneArea()
{
	return sceneArea;
}
void cScene::Move(int pointer)
{
	//map=32x32, visible=20x17 => move=0..32-20,0..32-17=0..12,0..15

	//Up
	if(pointer==MN || pointer==MNO || pointer==MNE)
	{
		if(cy > 0) --cy;
	}
	//South
	else if(pointer==MS || pointer==MSO || pointer==MSE )
	{
		if(cy < sceneArea-SCENE_HEIGHT) ++cy;
	}
	//West
	if(pointer==MO || pointer==MNO || pointer==MSO)
	{
		if(cx > 0) --cx;
	}
	//East
	else if(pointer==ME || pointer==MNE || pointer==MSE)
	{
		if(cx < sceneArea-SCENE_WIDTH) ++cx;
	}
}

void cScene::MoveByRadar(int x, int y)
{
	//move=0..12,0..15
	//rect=(80x68)/4=20x17

	cx = (x-RADAR_Xo)>>2;
	cy = (y-RADAR_Yo)>>2;

	if(cx <= 9)			cx=0;
	else if(cx >= 21)	cx=12;
	else				cx-=9;
	
	if(cy <= 8)			cy=0;
	else if(cy >= 23)	cy=15;
	else				cy-=8;
}

bool cScene::Visible(int cellx,int celly)
{
	return (cellx>=cx && cellx<cx+SCENE_WIDTH && celly>=cy && celly<cy+SCENE_HEIGHT) ? 1 : 0;
}

void cScene::Update()
{
	++waterDelay;
	if(waterDelay>5)
	{
		++waterAnimation;
		if(waterAnimation == 34) waterAnimation = 0;
		waterDelay = 0;
	}
}

int cScene::GetTileIndex(int x, int y)
{
	return y*sceneArea +x;
}

int cScene::GetSelectedTileIndex()
{
	return sely*sceneArea+selx;
}